<?php
/*
* db.class.php - Database laziness layer.
*
* :tabSize=4:indentSize=4:noTabs=false:
* :folding=explicit:collapseFolds=1:
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Class: db.class.php
*
* Database laziness layer.
*
* This is a minimal abstraction layer providing an interface with MySQL.
* The main purpose of this layer is to take care of all the little details
* that I'd rather not do over and over again in other places, like result
* error-checking and query debugging. If for some reason we decide to move
* to another database platform in the future, this will also make the
* switch easy.
*/
class db {
# Group: Variables
/**
* String: server
*
* Hostname of the MySQL server.
*/
	var $server;
/**
* String: user
*
* MySQL username.
*/
	var $user;
/**
* String: pass
*
* MySQL password.
*/
	var $pass;
/**
* String: database
*
* Name of the MySQL database.
*/
	var $database;
	var $conn_db;
/**
* Bool: conected
*
* *true* if a db connection is open, *false* otherwise.
*/
	var $conected;
/**
* Handle: handle
*
* Set to a MySQL connection handle when a connection is opened.
*/
	var $handle;
	function db() {
		$this->connected = false;
	}
# Group: Functions
/**
* Function: connect
*
* Opens a connection to the database and selects the database for use.
* If a connection is already open, that connection will be reused.
*
* Returns:
*
* *true* on success, *false* on failure.
*/
	function connect() {
		if ($this->conected == false || $this->conn_db != $this->database) {
			$this->handle = mysql_pconnect($this->server, $this->user, $this->pass);
			if ($this->handle == false) {
				$this->conected = false;
				echo _DEBUG ? "Could not connect to database.<br/>\n" : "";
				return false;
			} else {
				$this->conected = true;
				if (mysql_select_db($this->database, $this->handle)) {
					$this->conn_db = $this->database;
					return true;
				} else {
					echo _DEBUG ? mysql_error($this->handle)."<br/>\n" : "";
				}
			}
		} else {
			return true;
		}

	}
/**
* Function: execute
*
* Alias for <query>.
*
* See Also:
*
* <query>
*/
	function execute($query) {
		return $this->query($query);
	}
/**
* Function: query
*
* Executes a database query and returns the results.
*
* Parameters:
*
* query - SQL query to execute.
*
* Returns:
*
* The returned data vary depending on the fetch mode specified, but
* by default an array of row objects will be returned. If the query
* is of a type that doesn't return a result set, the return value
* will be a boolean indicating whether the query executed
* successfully.
*
* See Also:
*
* <execute>
*/
	function query($query) {
		if ($this->connect()) {
			//echo _BEHINDTHEMAGIC ? "<code>$query</code><br />\n" : "";
			$result = mysql_query($query, $this->handle);
			if ($result === false) {
				echo _DEBUG ? mysql_error($this->handle)."<br />\n": "";
				return false;
			} else {
				if ($result === true) {
					return true;
				} elseif ($result !== false) {
					$rows = array();
					while ($row = mysql_fetch_object($result))
						$rows[] = $row;
					mysql_free_result($result);
					return $rows;
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}


}
?>